<template>
  <el-dialog :title="dialogTitle" :visible="showDialog" @close="closeFn">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="2-10个字符"
          style="width: 80%"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          placeholder="2-10个字符"
          style="width: 80%"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          style="width: 80%"
          v-model="form.managerId"
          placeholder="请选择负责人"
        >
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          type="textarea"
          :rows="4"
          style="width: 80%"
          placeholder="1-100个字符"
          v-model="form.introduce"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="submit">确 定</el-button>
            <el-button size="mini" @click="closeFn">取 消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getManagerListApi, addDepsApi, getDepsDetailApi, editDepsApi } from '@/api/department'
export default {
  props:{
    showDialog:Boolean,
    originDeps:Array,
    currentNodeId:[Number,String]
  },
  created(){
    this.getManagerList()
  },
  data() {
    return {
      form: {
        name:'',
        code:'',
        managerId:'',
        introduce:''
      },
      managerList:[],
      rules: {
        name:[
          { required:true, message:'请输入部门名称', trigger: 'blur'},
          { min:2, max:10, message:'要求2-10个字符', trigger: 'blur'},
          { validator: (rule, value, callback) => {
            let deps = this.originDeps
            // 如果form里面有请求详情接口时返回的id，那就是在编辑，把自己筛出去
            if(this.form.id){
              deps = deps.filter(item => item.id !== this.form.id)
            }
            const res = deps.some(item => item.name === value)
            res ? callback(new Error('部门名称已存在')) : callback()
          }, 
          trigger: 'blur'}
          ],
        code:[
          { required:true, message:'请输入部门编码', trigger: 'blur'},
          { min:2, max:10, message:'要求2-10个字符', trigger: 'blur'},
          { validator: (rule, value, callback) => {
            const res = this.originDeps.some(item => {
              // 如果编码和目前所有部门都不重合，那就返回false，通过校验
              // 如果编码有和目前某部门重合，并且就是它自己，那就相当于在编辑，同样返回false，通过校验
              // 如果编码有和目前某部门重合，并且不是他自己，那就跟别人撞到了，返回true，校验不通过
              // 新增时，this.form.id 是 undefined，所以 item.id 肯定不等于 undefined，这时候就只判断是否和别人重合，不重合就返回false，通过校验
              return item.code === value && item.id !== this.form.id
          })
            res ? callback(new Error('部门编码已存在')) : callback()
          }, 
          trigger: 'blur'}
          ],
        managerId:[
          { required:true, message:'请选择部门负责人', trigger: 'change'},
          ],
        introduce:[
          { required:true, message:'请输入部门介绍', trigger: 'blur'},
          { min:1, max:100, message:'要求2-10个字符', trigger: 'blur'}
          ],
      }
    };
  },
  computed:{
    dialogTitle(){
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods:{
    closeFn(){
      // resetFields 只能重置双向绑定的数据
      this.$refs.addForm.resetFields();
      // 所以手动清空一下
      this.form = {
        name:'',
        code:'',
        managerId:'',
        introduce:''
      }
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    async getManagerList(){
      const res = await getManagerListApi()
      this.managerList = res
    },
    submit(){
      this.$refs.addForm.validate(async pass => {
        if(pass){
          // 判断新增还是编辑
          this.form.id 
          ? await editDepsApi({...this.form}) 
          : await addDepsApi({
            ...this.form, pid:this.currentNodeId
          })
          this.closeFn()
          // 更新列表
          this.$emit('updateDeps')
        }
      })
    },
    async getDepsDetail() {
      const res = await getDepsDetailApi(this.currentNodeId)
      console.log(res);
      this.form = {...res}
    }
  }
};
</script>

<style></style>
